package main

import (
	"config-client-v1/utils/tools"
	"flag"
	"fmt"
	"github.com/jacobsa/go-serial/serial"
	"io"
	"time"
)

var Port io.ReadWriteCloser

func main() {
	portName := flag.String("port_name", "COM7", "串口名")
	baudRate := flag.Int("baud_rate", 9600, "波特率")
	flag.Parse()
	options := serial.OpenOptions{
		PortName:        *portName,
		BaudRate:        uint(*baudRate),
		DataBits:        8,
		StopBits:        1,
		MinimumReadSize: 4,
		ParityMode:      serial.ParityMode(0),
	}

	port, err := serial.Open(options)
	if err != nil {
		fmt.Printf("串口打开失败 %s %v\n", *portName, err.Error())
		return
	}
	fmt.Printf("串口打开成功 %s\n", *portName)
	Port = port
	defer func() {
		_ = port.Close()
	}()
	if *portName == "COM7" {
		go writeTestCOM9()
	}
	if *portName == "COM11" {
		go writeTestCOM11()
	}
	// 循环读取数据
	for {
		buffer := make([]byte, 4096) // 缓冲区大小
		n, err2 := port.Read(buffer)
		if err2 != nil {
			fmt.Printf("port.Read error %s\n", err2.Error())
			continue
		}
		// 输出读到的数据
		if n > 0 {
			time.Sleep(5 * time.Millisecond)
			recvData := tools.ByteArrayToHexStr(buffer[:n])
			fmt.Printf("len=%d %v\n", n, recvData)
		}
	}
}

// COM9 端口模拟发送数据
func writeTestCOM9() {
	for {
		//rawData := "5A0101"
		//rawData += tools.ByteToHexStr(byte(tools.GetRandAreaNumber(0, 1)))
		//rawData += tools.Uint16ToHexStr(uint16(tools.GetRandAreaNumber(200, 250)), "big")
		//rawData += tools.Float32ToHexStr(float32(tools.GetRandAreaNumber(8000, 9000))/float32(100.0), "big")
		//rawData += tools.ByteToHexStr(byte(tools.GetRandAreaNumber(0, 1)))
		//rawData += tools.ByteToHexStr(byte(tools.GetRandAreaNumber(0, 1)))
		//data, _ := tools.HexStrToByteArray(rawData)
		//data = tools.AppendCrc16(data)

		data := []byte{0xA5, 0x01, 0x01}
		f1 := float32(tools.GetRandAreaNumber(1000000, 9999999)) / float32(10000)
		f2 := float32(tools.GetRandAreaNumber(1000000, 9999999)) / float32(10000)
		b1 := tools.Float32ToBytes(f1, "big")
		b2 := tools.Float32ToBytes(f2, "big")
		fmt.Printf("f1=%v, b1=%v\n", f1, b1)
		data = append(data, b1...)
		data = append(data, b2...)
		data = append(data, 0x5A)
		n, err := Port.Write(data)
		fmt.Printf("【%s】n=%d err=%v\n", tools.GetNow(), n, err)
		time.Sleep(time.Second * 1)
	}
}

// COM11 端口模拟发送数据
func writeTestCOM11() {
	for {
		rawData := "A5"
		rawData += tools.Float32ToHexStr(float32(tools.GetRandAreaNumber(0, 500))/float32(100.0), "big")
		rawData += tools.Float32ToHexStr(float32(tools.GetRandAreaNumber(500, 1500))/float32(100.0), "big")
		rawData += tools.Float32ToHexStr(float32(tools.GetRandAreaNumber(3000, 3500))/float32(100.0), "big")
		rawData += tools.Float32ToHexStr(float32(tools.GetRandAreaNumber(3500, 4000))/float32(100.0), "big")
		rawData += tools.Float32ToHexStr(float32(tools.GetRandAreaNumber(0, 500))/float32(100.0), "big")
		rawData += tools.Float32ToHexStr(float32(tools.GetRandAreaNumber(500, 3000))/float32(100.0), "big")
		rawData += tools.Float32ToHexStr(float32(tools.GetRandAreaNumber(6000, 7000))/float32(100.0), "big")
		rawData += tools.Float32ToHexStr(float32(tools.GetRandAreaNumber(4000, 6000))/float32(100.0), "big")
		data, _ := tools.HexStrToByteArray(rawData)
		data = tools.AppendCrc16(data)
		n, err := Port.Write(data)
		fmt.Printf("【%s】n=%d err=%v\n", tools.GetNow(), n, err)
		time.Sleep(time.Second * 1)
	}
}
